package com.wn.ziroom.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Getter;
import lombok.Setter;

/**
 * <p>
 *
 * </p>
 *
 * @author wangzhijia
 * @since 2023-02-23 02:27:54
 */
@Getter
@Setter
@TableName("t_cart")
public class Cart {

    /**
     * ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    //-- 用户编号
    @TableField("user_id")
    private Integer userId;

    //-- 合计
    @TableField("total")
    private BigDecimal total;

    //-- 购物车明细
    private List<CartItem> cartItemList = new ArrayList<>();



    public List<CartItem> getCartItemList() {
        return cartItemList;
    }

    public void setCartItemList(List<CartItem> cartItemList) {
        this.cartItemList = cartItemList;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public BigDecimal getTotal() {

        //--  弊端：缺少选中！
        for (CartItem cartItem : cartItemList) {
            BigDecimal subTotal = cartItem.getSubTotal();
            total = total.add(subTotal);
        }

        return total;
    }

    public void setTotal(BigDecimal total) {
        this.total = total;
    }

    @Override
    public String toString() {
        return "Cart{" +
                "cartItemSet=" + cartItemList +
                ", userId=" + userId +
                ", total=" + total +
                '}';
    }
}
